package bday.tvlport.beans;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import bday.tvlport.models.Image;

@ManagedBean(name = "galleriaBean")
@RequestScoped
public class GalleriaBean {

    private List<Image> images;
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();

    @PostConstruct
    public void init() {
        images = new ArrayList<Image>();
        //File result = new File(extContext.getRealPath("//WEB-INF//photos//"));
        File result = new File("photos/");
        if(result.isDirectory()) {
        	File[] imgs = result.listFiles();
        	if(imgs != null) {
        		for (File file : imgs) {
        			Image image = new Image();
        			image.setPath(file.getPath());
        			String fileName = file.getName();
        			String name = fileName.substring(fileName.indexOf("_")+1,fileName.lastIndexOf("."));
        			String date = fileName.substring(0, fileName.indexOf("T"));
        			image.setAlt(name);
        			image.setDesc("Birthday celebration of " + name + " on " + date);
        			images.add(image);
				}
        	}
        }
    }
   
    public List<Image> getImages() {
        return images;
    }
}